package ${daoPackageName}.impl;

import ${modelPackageName}.${modelName?cap_first};
import ${daoPackageName}.I${modelName?cap_first}Dao;
import net.ymate.platform.core.beans.annotation.Bean;
import net.ymate.platform.persistence.Fields;
import net.ymate.platform.persistence.IResultSet;
import net.ymate.platform.persistence.Page;
import net.ymate.platform.persistence.jdbc.ISession;
import net.ymate.platform.persistence.jdbc.ISessionExecutor;
import net.ymate.platform.persistence.jdbc.JDBC;
import net.ymate.platform.persistence.jdbc.query.Cond;
import net.ymate.platform.persistence.jdbc.query.Where;
import java.util.List;

/**
* @Author: CurdGenerateImpl.
* @Date: ${lastUpdateTime?string("yyyy/MM/dd")}.
* @Time: ${lastUpdateTime?string("HH:mm:ss")}.
* @Description: ${modelName?cap_first}DaoImpl generated By CurdGenerateImpl on ${lastUpdateTime?string("yyyy/MM/dd a HH:mm:ss")}
*/
@Bean
public class ${modelName?cap_first}DaoImpl implements I${modelName?cap_first}Dao {

    /**
    * 添加
    *
    * @param ${modelName?uncap_first} 添加的数据信息
    * @return 添加成功的数据信息
    * @throws Exception 添加异常
    */
    @Override
    public ${modelName?cap_first} create(${modelName?cap_first} ${modelName?uncap_first}) throws Exception {
        return ${modelName?uncap_first}.save();
    }

    /**
    * 修改
    *
    * @param ${modelName?uncap_first} 修改的数据信息
    * @param fields                   要修改哪些字段
    * @return 修改成功的数据信息
    * @throws Exception 修改异常
    */
    @Override
    public ${modelName?cap_first} update(${modelName?cap_first} ${modelName?uncap_first}, String... fields) throws Exception {
        return ${modelName?uncap_first}.update(Fields.create(fields));
    }

    /**
    * 删除
    *
    * @param ${modelName?uncap_first}List 要删除的集合
    * @throws Exception 删除异常
    */
    @Override
    public void delete(final List<${modelName?cap_first}> ${modelName?uncap_first}List) throws Exception {
        JDBC.get().openSession(new ISessionExecutor<Object>() {
            @Override
            public Object execute(ISession session) throws Exception {
                return session.delete(${modelName?uncap_first}List);
            }
        });
    }

    /**
    * 根据id查询
    *
    * @param id     数据id
    * @param fields 要查询哪些字段
    * @return 查询的数据
    * @throws Exception 查询异常
    */
    @Override
    public ${modelName?cap_first} findById(String id, String... fields) throws Exception {
        return ${modelName?cap_first}.builder().id(id).build().load(Fields.create(fields));
    }

    /**
    * 查询所有
    *
    <#list fieldList as field>
        *@param ${field.varName} ${field.remarks}
    </#list>
    * @param page     第几页
    * @param pageSize 每页显示多少条
    * @return 查询的数据
    * @throws Exception 查询异常
    */
    @Override
    public IResultSet<${modelName?cap_first}> findAll(<#list fieldList as field>${field.varType} ${field.varName}<#if field_has_next>, </#if></#list>, int page, int pageSize) throws Exception {
        Cond cond = Cond.create().eqOne();
        <#list fieldList as field>
            cond.exprNotEmpty(${field.varName}, Cond.create().and().eq(${modelName?cap_first}.FIELDS.${field.columnName}).param(${field.varName}));
        </#list>
        return ${modelName?cap_first}.builder().build().find(Where.create(cond),Page.create(page).pageSize(pageSize));
    }
}
